package com.jmeter.functions;

import com.shenbianyun.bosskgpay.constant.MerInfoConstant;
import com.shenbianyun.bosskgpay.utils.Base64Utils;
import com.shenbianyun.bosskgpay.utils.DESUtils;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author yvan
 * @Date 2021/11/22 10:17:39
 * @Description //业务参数字符串加密
 **/
public class DESFunctions extends AbstractFunction {

    public static  String key = "__DESJmeter";
    public static  List<String> params = new LinkedList<>();
    static {
        params.add("请输入要加密的json串");
    }
    public String reqData;



    @Override
    public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {

        String reqDataEncrypt;
        try {

            //reqData加密
            byte[] bs = DESUtils.encrypt(reqData.getBytes(MerInfoConstant.CHARSET_UTF_8),MerInfoConstant.INTER_KEY);
            reqDataEncrypt = Base64Utils.encodeToString(bs);
            System.out.println(reqDataEncrypt);
        } catch (Exception e) {
            e.printStackTrace();
            reqDataEncrypt = "加密失败";
        }
        return reqDataEncrypt;
    }

    @Override
    public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {
        Object[] objects = collection.toArray();
        reqData = ((CompoundVariable)objects[0]).execute();
    }

    @Override
    public String getReferenceKey() {
        return key;
    }

    @Override
    public List<String> getArgumentDesc() {
        return params;
    }
}
